<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.19"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Modbus cpp: include/MB/modbusException.hpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">Modbus cpp
   &#160;<span id="projectnumber">0.1</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.19 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_d44c64559bbebec7f509842c48db8b23.html">include</a></li><li class="navelem"><a class="el" href="dir_f8aebf652183de33714d471cbd098255.html">MB</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">modbusException.hpp</div>  </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="comment">// Modbus for c++ &lt;https://github.com/Mazurel/Modbus&gt;</span></div>
<div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="comment">// Copyright (c) 2020 Mateusz Mazur aka Mazurel</span></div>
<div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="comment">// Licensed under: MIT License &lt;http://opensource.org/licenses/MIT&gt;</span></div>
<div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160; </div>
<div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;<span class="preprocessor">#pragma once</span></div>
<div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160; </div>
<div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;<span class="preprocessor">#include &lt;cstdint&gt;</span></div>
<div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;<span class="preprocessor">#include &lt;cstring&gt;</span></div>
<div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;<span class="preprocessor">#include &lt;memory&gt;</span></div>
<div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;<span class="preprocessor">#include &lt;optional&gt;</span></div>
<div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;<span class="preprocessor">#include &lt;stdexcept&gt;</span></div>
<div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;<span class="preprocessor">#include &lt;vector&gt;</span></div>
<div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160; </div>
<div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;<span class="preprocessor">#include &quot;modbusUtils.hpp&quot;</span></div>
<div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160; </div>
<div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;<span class="keyword">namespace </span><a class="code" href="namespaceMB.html">MB</a> {</div>
<div class="line"><a name="l00025"></a><span class="lineno"><a class="line" href="classMB_1_1ModbusException.html">   25</a></span>&#160;<span class="keyword">class </span><a class="code" href="classMB_1_1ModbusException.html">ModbusException</a> : <span class="keyword">public</span> std::exception {</div>
<div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;<span class="keyword">private</span>:</div>
<div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;  uint8_t _slaveId;</div>
<div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;  <span class="keywordtype">bool</span> _validSlave;</div>
<div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;  <a class="code" href="namespaceMB_1_1utils.html#aa34c557c9bf4c6bfeb86aa16b398de55">utils::MBErrorCode</a> _errorCode;</div>
<div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160;  <a class="code" href="namespaceMB_1_1utils.html#aa8ebb89b73bf4d0e3485b6341a50f116">utils::MBFunctionCode</a> _functionCode;</div>
<div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160; </div>
<div class="line"><a name="l00032"></a><span class="lineno">   32</span>&#160;<span class="keyword">public</span>:</div>
<div class="line"><a name="l00041"></a><span class="lineno">   41</span>&#160;  <span class="keyword">explicit</span> <a class="code" href="classMB_1_1ModbusException.html#a27fc098093dfa4b7ff49acebb5c4a0bf">ModbusException</a>(<span class="keyword">const</span> std::vector&lt;uint8_t&gt; &amp;inputData,</div>
<div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;                           <span class="keywordtype">bool</span> CRC = <span class="keyword">false</span>) noexcept;</div>
<div class="line"><a name="l00043"></a><span class="lineno">   43</span>&#160; </div>
<div class="line"><a name="l00045"></a><span class="lineno"><a class="line" href="classMB_1_1ModbusException.html#a2c60c1d081060a266a3f868698a289b6">   45</a></span>&#160;  <span class="keyword">explicit</span> <a class="code" href="classMB_1_1ModbusException.html#a2c60c1d081060a266a3f868698a289b6">ModbusException</a>(</div>
<div class="line"><a name="l00046"></a><span class="lineno">   46</span>&#160;      <a class="code" href="namespaceMB_1_1utils.html#aa34c557c9bf4c6bfeb86aa16b398de55">utils::MBErrorCode</a> errorCode, uint8_t slaveId = 0xFF,</div>
<div class="line"><a name="l00047"></a><span class="lineno">   47</span>&#160;      <a class="code" href="namespaceMB_1_1utils.html#aa8ebb89b73bf4d0e3485b6341a50f116">utils::MBFunctionCode</a> functionCode = utils::Undefined) noexcept</div>
<div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160;      : _slaveId(slaveId), _validSlave(<span class="keyword">true</span>), _errorCode(errorCode),</div>
<div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;        _functionCode(functionCode) {}</div>
<div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160; </div>
<div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;  <span class="comment">/*</span></div>
<div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;<span class="comment">   * Check if there is Modbus error in raw modbus input</span></div>
<div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;<span class="comment">   * NOTE: this method doesn&#39;t detect invalid byte order, byte order is</span></div>
<div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;<span class="comment">   * checked at ModbusRequest/ModbusResponse</span></div>
<div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;<span class="comment">   * */</span></div>
<div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;  <span class="keyword">static</span> <span class="keywordtype">bool</span> exist(<span class="keyword">const</span> std::vector&lt;uint8_t&gt; &amp;inputData) noexcept {</div>
<div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160;    <span class="keywordflow">if</span> (inputData.size() &lt; 2) <span class="comment">// TODO Figure out better solution to such mistake</span></div>
<div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;      <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160; </div>
<div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;    <span class="keywordflow">return</span> inputData[1] &amp; 0b10000000;</div>
<div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;  }</div>
<div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160; </div>
<div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;  <span class="comment">/*</span></div>
<div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;<span class="comment">   *  Returns attached SlaveID</span></div>
<div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;<span class="comment">   *  NOTE: it is worth to check if slaveId is specified with isSlaveValid()</span></div>
<div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;<span class="comment">   * */</span></div>
<div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;  [[nodiscard]] uint8_t slaveID() const noexcept { <span class="keywordflow">return</span> _slaveId; }</div>
<div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160; </div>
<div class="line"><a name="l00070"></a><span class="lineno"><a class="line" href="classMB_1_1ModbusException.html#a2d93520088d9adac402b4558a7441d80">   70</a></span>&#160;  [[nodiscard]] <span class="keywordtype">bool</span> <a class="code" href="classMB_1_1ModbusException.html#a2d93520088d9adac402b4558a7441d80">isSlaveValid</a>() const noexcept { <span class="keywordflow">return</span> _validSlave; }</div>
<div class="line"><a name="l00071"></a><span class="lineno">   71</span>&#160; </div>
<div class="line"><a name="l00073"></a><span class="lineno"><a class="line" href="classMB_1_1ModbusException.html#a344445f1b157f5e5d66946cd893c72fd">   73</a></span>&#160;  <span class="keywordtype">void</span> <a class="code" href="classMB_1_1ModbusException.html#a344445f1b157f5e5d66946cd893c72fd">setSlaveID</a>(uint8_t slaveId) noexcept {</div>
<div class="line"><a name="l00074"></a><span class="lineno">   74</span>&#160;    _validSlave = <span class="keyword">true</span>;</div>
<div class="line"><a name="l00075"></a><span class="lineno">   75</span>&#160;    _slaveId = slaveId;</div>
<div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160;  }</div>
<div class="line"><a name="l00077"></a><span class="lineno">   77</span>&#160; </div>
<div class="line"><a name="l00079"></a><span class="lineno"><a class="line" href="classMB_1_1ModbusException.html#a99b4583ae15c2a13530e3751e9ad2737">   79</a></span>&#160;  [[nodiscard]] <a class="code" href="namespaceMB_1_1utils.html#aa34c557c9bf4c6bfeb86aa16b398de55">utils::MBErrorCode</a> <a class="code" href="classMB_1_1ModbusException.html#a99b4583ae15c2a13530e3751e9ad2737">getErrorCode</a>() const noexcept {</div>
<div class="line"><a name="l00080"></a><span class="lineno">   80</span>&#160;    <span class="keywordflow">return</span> _errorCode;</div>
<div class="line"><a name="l00081"></a><span class="lineno">   81</span>&#160;  }</div>
<div class="line"><a name="l00082"></a><span class="lineno">   82</span>&#160; </div>
<div class="line"><a name="l00087"></a><span class="lineno"><a class="line" href="classMB_1_1ModbusException.html#acddacc2b4de0b5e85275b8d1bfa67c4f">   87</a></span>&#160;  [[nodiscard]] <span class="keyword">const</span> <span class="keywordtype">char</span> *<a class="code" href="classMB_1_1ModbusException.html#acddacc2b4de0b5e85275b8d1bfa67c4f">what</a>() const noexcept<span class="keyword"> override </span>{</div>
<div class="line"><a name="l00088"></a><span class="lineno">   88</span>&#160;    <span class="keyword">auto</span> og = <a class="code" href="classMB_1_1ModbusException.html#a29d776e7ae321fbeba28a25df5159c79">toString</a>();</div>
<div class="line"><a name="l00089"></a><span class="lineno">   89</span>&#160;    <span class="keywordtype">char</span> *str = <span class="keyword">new</span> <span class="keywordtype">char</span>[og.size()];</div>
<div class="line"><a name="l00090"></a><span class="lineno">   90</span>&#160;    stpcpy(str, og.c_str());</div>
<div class="line"><a name="l00091"></a><span class="lineno">   91</span>&#160;    <span class="keywordflow">return</span> str;</div>
<div class="line"><a name="l00092"></a><span class="lineno">   92</span>&#160;  }</div>
<div class="line"><a name="l00093"></a><span class="lineno">   93</span>&#160; </div>
<div class="line"><a name="l00095"></a><span class="lineno">   95</span>&#160;  [[nodiscard]] std::string <a class="code" href="classMB_1_1ModbusException.html#a29d776e7ae321fbeba28a25df5159c79">toString</a>() const noexcept;</div>
<div class="line"><a name="l00097"></a><span class="lineno">   97</span>&#160;  [[nodiscard]] std::vector&lt;uint8_t&gt; <a class="code" href="classMB_1_1ModbusException.html#a3f288bfe0b2b0f646ab0019faae78142">toRaw</a>() const noexcept;</div>
<div class="line"><a name="l00098"></a><span class="lineno">   98</span>&#160; </div>
<div class="line"><a name="l00099"></a><span class="lineno">   99</span>&#160;  [[nodiscard]] utils::MBFunctionCode functionCode() const noexcept {</div>
<div class="line"><a name="l00100"></a><span class="lineno">  100</span>&#160;    <span class="keywordflow">return</span> _functionCode;</div>
<div class="line"><a name="l00101"></a><span class="lineno">  101</span>&#160;  }</div>
<div class="line"><a name="l00102"></a><span class="lineno">  102</span>&#160; </div>
<div class="line"><a name="l00103"></a><span class="lineno">  103</span>&#160;  <span class="keywordtype">void</span> setFunctionCode(<a class="code" href="namespaceMB_1_1utils.html#aa8ebb89b73bf4d0e3485b6341a50f116">utils::MBFunctionCode</a> functionCode) noexcept {</div>
<div class="line"><a name="l00104"></a><span class="lineno">  104</span>&#160;    _functionCode = functionCode;</div>
<div class="line"><a name="l00105"></a><span class="lineno">  105</span>&#160;  }</div>
<div class="line"><a name="l00106"></a><span class="lineno">  106</span>&#160;};</div>
<div class="line"><a name="l00107"></a><span class="lineno">  107</span>&#160;} <span class="comment">// namespace MB</span></div>
</div><!-- fragment --></div><!-- contents -->
<div class="ttc" id="aclassMB_1_1ModbusException_html_a29d776e7ae321fbeba28a25df5159c79"><div class="ttname"><a href="classMB_1_1ModbusException.html#a29d776e7ae321fbeba28a25df5159c79">MB::ModbusException::toString</a></div><div class="ttdeci">std::string toString() const noexcept</div><div class="ttdoc">Returns string representation of object.</div><div class="ttdef"><b>Definition:</b> modbusException.cpp:36</div></div>
<div class="ttc" id="aclassMB_1_1ModbusException_html_a2c60c1d081060a266a3f868698a289b6"><div class="ttname"><a href="classMB_1_1ModbusException.html#a2c60c1d081060a266a3f868698a289b6">MB::ModbusException::ModbusException</a></div><div class="ttdeci">ModbusException(utils::MBErrorCode errorCode, uint8_t slaveId=0xFF, utils::MBFunctionCode functionCode=utils::Undefined) noexcept</div><div class="ttdoc">Constructs Exception based on error code, function code and slaveId.</div><div class="ttdef"><b>Definition:</b> modbusException.hpp:45</div></div>
<div class="ttc" id="aclassMB_1_1ModbusException_html_a99b4583ae15c2a13530e3751e9ad2737"><div class="ttname"><a href="classMB_1_1ModbusException.html#a99b4583ae15c2a13530e3751e9ad2737">MB::ModbusException::getErrorCode</a></div><div class="ttdeci">utils::MBErrorCode getErrorCode() const noexcept</div><div class="ttdoc">Returns detected error code.</div><div class="ttdef"><b>Definition:</b> modbusException.hpp:79</div></div>
<div class="ttc" id="anamespaceMB_1_1utils_html_aa8ebb89b73bf4d0e3485b6341a50f116"><div class="ttname"><a href="namespaceMB_1_1utils.html#aa8ebb89b73bf4d0e3485b6341a50f116">MB::utils::MBFunctionCode</a></div><div class="ttdeci">MBFunctionCode</div><div class="ttdoc">All modbus standard function codes + Undefined one.</div><div class="ttdef"><b>Definition:</b> modbusUtils.hpp:114</div></div>
<div class="ttc" id="anamespaceMB_1_1utils_html_aa34c557c9bf4c6bfeb86aa16b398de55"><div class="ttname"><a href="namespaceMB_1_1utils.html#aa34c557c9bf4c6bfeb86aa16b398de55">MB::utils::MBErrorCode</a></div><div class="ttdeci">MBErrorCode</div><div class="ttdef"><b>Definition:</b> modbusUtils.hpp:22</div></div>
<div class="ttc" id="aclassMB_1_1ModbusException_html_acddacc2b4de0b5e85275b8d1bfa67c4f"><div class="ttname"><a href="classMB_1_1ModbusException.html#acddacc2b4de0b5e85275b8d1bfa67c4f">MB::ModbusException::what</a></div><div class="ttdeci">const char * what() const noexcept override</div><div class="ttdef"><b>Definition:</b> modbusException.hpp:87</div></div>
<div class="ttc" id="aclassMB_1_1ModbusException_html_a27fc098093dfa4b7ff49acebb5c4a0bf"><div class="ttname"><a href="classMB_1_1ModbusException.html#a27fc098093dfa4b7ff49acebb5c4a0bf">MB::ModbusException::ModbusException</a></div><div class="ttdeci">ModbusException(const std::vector&lt; uint8_t &gt; &amp;inputData, bool CRC=false) noexcept</div><div class="ttdoc">Constructs Exception from raw data.</div><div class="ttdef"><b>Definition:</b> modbusException.cpp:10</div></div>
<div class="ttc" id="aclassMB_1_1ModbusException_html_a344445f1b157f5e5d66946cd893c72fd"><div class="ttname"><a href="classMB_1_1ModbusException.html#a344445f1b157f5e5d66946cd893c72fd">MB::ModbusException::setSlaveID</a></div><div class="ttdeci">void setSlaveID(uint8_t slaveId) noexcept</div><div class="ttdoc">Sets SlaveID.</div><div class="ttdef"><b>Definition:</b> modbusException.hpp:73</div></div>
<div class="ttc" id="aclassMB_1_1ModbusException_html_a3f288bfe0b2b0f646ab0019faae78142"><div class="ttname"><a href="classMB_1_1ModbusException.html#a3f288bfe0b2b0f646ab0019faae78142">MB::ModbusException::toRaw</a></div><div class="ttdeci">std::vector&lt; uint8_t &gt; toRaw() const noexcept</div><div class="ttdoc">Converts object to modbus byte representation.</div><div class="ttdef"><b>Definition:</b> modbusException.cpp:53</div></div>
<div class="ttc" id="aclassMB_1_1ModbusException_html_a2d93520088d9adac402b4558a7441d80"><div class="ttname"><a href="classMB_1_1ModbusException.html#a2d93520088d9adac402b4558a7441d80">MB::ModbusException::isSlaveValid</a></div><div class="ttdeci">bool isSlaveValid() const noexcept</div><div class="ttdoc">Checks if SlaveID is specified.</div><div class="ttdef"><b>Definition:</b> modbusException.hpp:70</div></div>
<div class="ttc" id="anamespaceMB_html"><div class="ttname"><a href="namespaceMB.html">MB</a></div><div class="ttdef"><b>Definition:</b> modbusCell.hpp:14</div></div>
<div class="ttc" id="aclassMB_1_1ModbusException_html"><div class="ttname"><a href="classMB_1_1ModbusException.html">MB::ModbusException</a></div><div class="ttdef"><b>Definition:</b> modbusException.hpp:25</div></div>
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by&#160;<a href="http://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.8.19
</small></address>
</body>
</html>
